package main

import (
	"fmt"
	"go_code/20210826/api"
	"go_code/20210826/pro"
)

var (
	result1 string
	result2 string
)

func init() {
	fmt.Println("此处执行的是main中的init方法")
	result1 = api.NameApi()
	result2 = pro.NamePro()
}

func main() {
	fmt.Printf("result1的值是：%v,result2的值是：%v\n", result1, result2)
	fmt.Println("---------------------------")
	MapMain()
	// sliceMain()
	// arrMain()
}

// MapMain 映射
func MapMain() {
	// 定义map 方式一
	var a map[int]string = make(map[int]string)
	a[2021082601] = "陈"
	a[2021082602] = "文"
	a[2021082603] = "小"
	a[2021082604] = "超"
	a[2021082605] = "石"
	a[2021082606] = "丽"
	a[2021082606] = "胜"
	a[2021082607] = "平"
	fmt.Println(a)
	fmt.Println("-----------------")

	// 定义map 方式二
	b := make(map[int]string)
	b[2021] = "1"
	b[2022] = "2"
	fmt.Println(b)
	fmt.Println("-----------")

	// 定义map 方式三
	c := map[int]string{
		202106: "小",
		202107: "超",
	}
	fmt.Println(c)
	fmt.Println("--------")
	map1 := make(map[string]map[int]string)
	// 赋值
	map1["班级1"] = make(map[int]string)
	map1["班级1"][2021082601] = "陈陈1"
	map1["班级1"][2021082602] = "文文1"
	map1["班级1"][2021082603] = "小小1"
	map1["班级1"][2021082604] = "超超1"

	map1["班级2"] = make(map[int]string)
	map1["班级2"][2021082601] = "陈陈2"
	map1["班级2"][2021082602] = "文文2"
	map1["班级2"][2021082603] = "小小2"
	map1["班级2"][2021082604] = "超超2"
	for k1, v1 := range map1 {
		for k2, v2 := range v1 {
			fmt.Printf("总共班级数为：%v,班级为：%v,共有学生：%v个,分别是：姓名为：%v，学号为：%v的学生\n", len(map1), k1, len(v1), v2, k2)
		}
	}

	fmt.Println("--------------")
	accountedInvoiceAfId := make([]int, 0)
	fmt.Printf("切片的长度为：%v,容量为：%v\n", len(accountedInvoiceAfId), cap(accountedInvoiceAfId))
}

// 切片
func sliceMain() {
	// 数组
	var intarr [6]int = [6]int{3, 6, 9, 1, 4, 7}
	fmt.Println(intarr)

	// 切片
	slice := intarr[1:3]
	fmt.Println(slice)
	fmt.Printf("slice的元素个数：%v\n", len(slice))
	fmt.Printf("slice的容量是：%v\n", cap(slice))
	fmt.Println("----------")
	fmt.Printf("数组中下标为1的元素的地址是：%p\n", &intarr[1])
	fmt.Printf("切片中下标为0的元素的地址是：%p\n", &slice[0])
	fmt.Println("----------")
	slice[0] = 16
	fmt.Println(intarr)
	fmt.Println(slice)
	fmt.Println("----------")

	// 创建切片
	slice1 := make([]int, 4, 20)
	fmt.Println(slice1)
	fmt.Printf("切片的长度是：%v\n", len(slice1))
	fmt.Printf("切片的容量是：%v\n", cap(slice1))
	fmt.Println("----------")

	// 遍历切片
	var arr3 [6]int = [6]int{1, 2, 3, 4, 5, 6}
	slice2 := arr3[0:6]

	// 遍历方式1
	for i := 0; i < len(slice2); i++ {
		fmt.Printf("切片的第%v个值是：%v\n", i+1, slice2[i])
	}

	fmt.Println("--------------")

	for key, value := range slice2 {
		fmt.Printf("slice2[%v]=%v\n", key, value)
	}
	fmt.Println(slice2)
	slice2 = append(slice2, 88, 50)

	fmt.Println(slice2)
}

// 数组
func arrMain() {
	var arr [2][3]int16

	fmt.Println(arr)
	fmt.Println("---------------------------")

	// 数组内存地址
	fmt.Printf("arr的内存地址是：%p\n", &arr)
	fmt.Printf("arr[0]的内存地址是：%p\n", &arr[0])
	fmt.Printf("arr[0][0]的内存地址是：%p\n", &arr[0][0])
	fmt.Println("---------------------------")

	// 初始化数组
	var arr1 [2][3]int = [2][3]int{{1, 2, 3}, {4, 5, 6}}
	fmt.Println(arr1)
	fmt.Println("---------------------------")

	// 变量数组
	var arr2 [3][3]int = [3][3]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

	// 方式1遍历
	for i := 0; i < len(arr2); i++ {
		for j := 0; j < len(arr2[i]); j++ {
			fmt.Printf("二维数组第%v层第%v列元素是：%v\n", i+1, j+1, arr2[i][j])
		}
	}
	fmt.Println("---------------------------")

	// 方式2编列
	for key, value := range arr2 {
		for k, v := range value {
			fmt.Printf("二维数组第%v层第%v列元素是：%v\n", key+1, k+1, v)
		}
	}
}
